package org.apache.dolphinscheduler.api.controller;

import static org.apache.dolphinscheduler.api.enums.Status.QUERY_RECHARGE_APPLY_FAIL;

import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.RechargeApplyService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.RechargeApply;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "RECHARGE_APPLY_TAG")
@RestController
@RequestMapping("/recharge-apply")
public class RechargeApplyController extends BaseController {
	
	@Autowired
	private RechargeApplyService rechargeApplyService;

	@Operation(summary = "saveRechargeApply", description = "SAVE_RECHARGE_APPLY")
    @PostMapping()
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_RECHARGE_APPLY_FAIL)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result<Object> saveRechargeApply(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, @RequestBody RechargeApply apply) {
		
        return rechargeApplyService.saveRechargeApply(loginUser, apply);
    }
	
	@Operation(summary = "auditRechargeApply", description = "AUDIT_RECHARGE_APPLY")
    @PostMapping(value = "/audit/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_RECHARGE_APPLY_FAIL)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result<Object> auditRechargeApply(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
    		@PathVariable("id") Integer id, @RequestBody RechargeApply apply) {
        return rechargeApplyService.auditRechargeApply(loginUser, id, apply);
    }
	
	@Operation(summary = "queryRechargeApplyList", description = "QUERY_RECHARGE_APPLY_LIST_NOTES")
    @Parameters({
            @Parameter(name = "pageNo", description = "PAGE_NO", required = true, schema = @Schema(implementation = int.class, example = "1")),
            @Parameter(name = "pageSize", description = "PAGE_SIZE", required = true, schema = @Schema(implementation = int.class, example = "10")),
            @Parameter(name = "userName", description = "USER_NAME", required = false),
            @Parameter(name = "beginTime", description = "BEGIN_TIME", required = false),
            @Parameter(name = "endTime", description = "END_TIME", required = false)
    })
    @GetMapping(value = "/list")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_RECHARGE_APPLY_FAIL)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result<Object> queryUserGroupList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                @RequestParam("pageNo") Integer pageNo,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam(value = "userName", required = false) String userName,
                                @RequestParam(value = "beginTime", required = false) String beginTime,
                                @RequestParam(value = "endTime", required = false) String endTime) {
        Result<Object> result = checkPageParams(pageNo, pageSize);
        if (!result.checkResult()) {
            return result;
        }
        return rechargeApplyService.queryAllRechargeApply(loginUser, pageNo, pageSize, userName, beginTime, endTime);
    }
}
